package cc.wsyw126.java.nowcoder.sword_offer;

/**
 * Created by Administrator on 2017/4/14.
 */
public class PrintMinNumber {
    public String PrintMinNumber(int[] numbers) {
        boolean change = false;
        for (int i = numbers.length - 1; i >= 0; i--) {
            for (int j = 0; j < i; j++) {
                StringBuffer stringBuffer = new StringBuffer();
                String ij = stringBuffer.append(numbers[j]).append(numbers[j + 1]).toString();
                stringBuffer.delete(0, stringBuffer.length());
                String ji = stringBuffer.append(numbers[j+1]).append(numbers[j]).toString();
                if (ij.compareTo(ji) > 0) {
                    int number = numbers[j];
                    numbers[j] = numbers[j+1];
                    numbers[j+1] = number;
                    change = true;
                }
            }
            if (!change)break;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < numbers.length; i++) {
            stringBuilder.append(numbers[i]);
        }
        return stringBuilder.toString();

    }

    public static void main(String[] args) {
        PrintMinNumber printMinNumber = new PrintMinNumber();
        String s = printMinNumber.PrintMinNumber(new int[]{3, 5, 1, 4, 2});
        System.out.println("s = " + s);
    }
}
